package com.smartstate.iot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.vo.TopicUpVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TopicLogMapper extends BaseMapper<TopicLog> {

    @Select("SELECT COUNT(*) FROM topic_log WHERE deviceId = #{deviceId}")
    Integer getLog(TopicUpVo.Content content);

    @Select("SELECT COUNT(*) FROM topic_log WHERE deviceId = #{deviceId}")
    Integer getDeviceIdOfOne(String deviceId);

    @Update("update topic_log set  logname = #{content.deviceName},deviceId =#{content.deviceId} ,type=#{topicLog.type},detail=#{topicLog.detail} where  deviceId = #{content.deviceId}")
    Boolean updateByDeviceId(TopicLog topicLog, TopicUpVo.Content content);

    @Update("update topic_log set type =#{topicLog.type},logname=#{topicLog.logName},date=#{topicLog.date},detail=#{topicLog.detail} where deviceId = #{deviceId}")
    Integer updateLogByDeviceId(String deviceId,TopicLog topicLog);

    @Select("SELECT * FROM topic_config;")
    List<TopicLog> getAllBox();

    @Select("SELECT * FROM topic_config x WHERE timesTamps = ( SELECT MAX(timesTamps) FROM topic_config y WHERE x.deviceId = y.deviceId ) ;")
    List<TopicLog> getAllDevice();

    @Select("SELECT * FROM topic_log x WHERE date = ( SELECT MAX(date) FROM topic_log y WHERE x.deviceId = y.deviceId ) AND deviceId = #{deviceId}")
    TopicLog getUpLogType(String deviceId);

    /**
     * 根据deviceId 查询盒子最新时间的状态
     */
    @Select("SELECT type FROM topic_log WHERE deviceId = #{s} order by date desc limit 1")
    String getTypeSize(String s);

    @Select("select id,type,logname,date,detail,deviceId from topic_log where deviceId = #{deviceId} order by date DESC limit 1")
    TopicLog getTypeByDeviceId(String deviceId);

    @Select("SELECT topic_log.type FROM topic_log WHERE deviceId = #{deviceId} ORDER BY date DESC LIMIT 1")
    String getGatewayInfo(String deviceId);

    @Select("select timesTamps from topic_config where deviceId =#{deviceId}")
    String getTimesTamps(String deviceId);

    @Insert(" INSERT INTO topic_log (deviceId,type,logname,detail) VALUES (#{deviceId},#{type},#{logname},#{detail})")
    void saveLog(TopicLog topicLog);

    @Delete("delete from topic_log where deviceId = #{deviceId}")
    void deleteOfDeviceId(String deviceId);
}
